package com.inspur.sc.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspur.sc.springboot.security.MyInvocationSecurityMetadataSourceService;
import com.inspur.sc.springboot.service.SysPermissionGroupService;
import com.inspur.sc.springboot.utils.ResponseUtil;
import com.inspur.sc.springboot.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/permissionGroup")
public class SysPermissionGroupController {

    @Autowired
    private SysPermissionGroupService permissionGroupService;

    @Autowired
    private MyInvocationSecurityMetadataSourceService myInvocationSecurityMetadataSourceService;

    @PostMapping("save")
    public ResponseEntity<ResponseVO> save(@RequestBody JSONObject data){
        Integer groupId=data.getInteger("groupId");
        List<Integer> menuNodes=data.getJSONArray("menuNodes").toJavaList(Integer.class);
        List<Integer> permissionNodes=data.getJSONArray("permissionNodes").toJavaList(Integer.class);
       boolean isSave=permissionGroupService.save(groupId, menuNodes,permissionNodes);
        if(isSave){
            //权限保存成后，对内存中的权限数据进行重置
            myInvocationSecurityMetadataSourceService.loadResourceDefine();
            return ResponseUtil.success("保存成功！",true);
        }
        return ResponseUtil.success("保存失败！",false);
    }


    @GetMapping("findBySysGroupId")
    public ResponseEntity<ResponseVO> findBySysGroupId(@RequestParam("id") Integer id){
        return ResponseUtil.success(permissionGroupService.findBySysGroupId(id));
    }
}
